#include <func.h>

int main(int argc, char* argv[])
{
	// 1. 先pipe
	int fields[2];
	if (pipe(fields) == -1) {
		error(1, errno, "pipe");
	}

	// 2. 再fork
	pid_t pid = fork();
	switch (pid) {
	case -1:
		error(1, errno, "fork");
	case 0:
		// 3. 子进程关闭管道的写端
		close(fields[1]);
		char message[256];
		read(fields[0], message, 256); // 阻塞点
		printf("Child: received '%s'\n", message);
		_exit(0);
	default: 
		// 4. 父进程关闭管道的读端
		close(fields[0]);
		sleep(5);
		write(fields[1],"hello from parent", 18);
		exit(0);
	}
    return 0;
}

